<?php
session_start();
include '../inc/connect_db.php';
$array_p_id = $_POST["p_id"];
$array_p_name = $_POST["p_name"];
$array_p_num_text = $_POST["p_num_text"];
$array_p_price= $_POST["p_price"];
$array_p_total = $_POST["p_total"];

// Insert data to bill
$bill_code = "ABC".rand(1, 10000);
$bill_date = date("Y-m-d H:i:s"); // mysql datetime format
$bill_customer_code = $_SESSION["user_id"];
$bill_staff_code = 100;
$bill_sum = array_sum($array_p_total);

$sql = "INSERT INTO tb_bill (bill_code,bill_date, 	bill_customer_code ,bill_staff_code,bill_sum) values ('".$bill_code."','".$bill_date."',$bill_customer_code,$bill_staff_code,$bill_sum) ";
mysql_query($sql);
$lasted_id = mysql_insert_id();// Get the id just inserted to DB
//Insert data to bill detail	
for ($i=0; $i <count($array_p_id) ; $i++) { 
	$p_id = $array_p_id[$i];
	$p_name = $array_p_name[$i];
	$p_num = $array_p_num_text[$i];
	$p_price = $array_p_price[$i];
	$p_total = $array_p_total[$i];
	
	$sql = "INSERT INTO 
			  tb_bill_detail 
			  (
				  bill_id,
				  product_id,
				  product_num,
				  bill_detail_total,
				  product_price
			  )
			   values (
			   '".$lasted_id."','".$p_id."',$p_num,$p_total,$p_price
			   ) ";
	mysql_query($sql);
}
echo 1;


